<?php

$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$setting=$this->plugin_setting('setting');
$tabs=array(
	array('op'=>'list','title'=>'区域代理等级管理'),
	array('op'=>'edit','title'=>'添加/编辑等级'),
);
set_html('tabs',$tabs);
if($_RQ['op']=='list'){
	$params=array('merchid'=>0,'uniacid'=>$_SESSION['uniacid']);
    $orderby='level asc';
	$total='';
	$return['list']=$this->getall('level',$params,array(),'',$orderby);
	foreach($return['list'] as &$item){
		$item['commissions']=$item['commissions']?json_decode($item['commissions'],true):array();
		$item['conditions']='默认等级';
	}
	//添加默认等级
	$default_level=array(
		'id'=>'0',
		'title'=>'默认等级',
		'level'=>0,
		'conditions'=>'默认等级',
		'commissions'=>$this->plugin_setting('default_level')?:array('commission1'=>'0.00'),
	);
	array_unshift($return['list'],$default_level);
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','edit'=>false,'list'=>array(
			array('name'=>'title','title'=>'等级名称','type'=>'string'),
			//array('name'=>'area_type','title'=>'适用代理类型','type'=>'radio','options'=>'area_types'),
			array('name'=>'level','title'=>'等级权重','type'=>'string'),
		),'action'=>array(
			array('op'=>'look_condition','title'=>'升级条件','type'=>'modal','show'=>array('level >'=>0)),
			array('op'=>'edit','title'=>'编辑','type'=>'href','show'=>array('level >'=>0)),
			array('op'=>'set_default','title'=>'设置默认分红比例','type'=>'href','show'=>array('level'=>0)),
		)),
	);
	if($this->setting['open_duodai']){
		$htmls['list']['list'][]=array('type'=>'string','name'=>'commissions.duodai','title'=>'多省代比例（%）');
	}
	for($i=1;$i<=$setting['level'];$i++){
		$htmls['list']['list'][]=array('type'=>'string','name'=>'commissions.commission'.$i,'title'=>$this->commission_money_types[$i].'比例（%）');
	}
	$return['htmls']=$htmls;
	//$return['area_types']=$this->area_types;
	exi($return);
}
if($_RQ['op']=='look_condition'){
	$item=$this->get('level',array('id'=>intval($_RQ['id']),'uniacid'=>$_SESSION['uniacid']),array('conditions'));
	if(!$item){
		exi('指定等级不存在！','error');
	}
	$item['conditions']=json_decode($item['conditions'],true);
	$return['item']=$item;
	if(!$item['conditions']){
		exi('该等级尚未设置升级条件！','error');
	}
	$htmls=array(
		'type'=>'look',
		'look'=>array(),
	);
	$level_up_types=$this->level_up_types;
	$current_level_up_types=$this->model()->current_level_up_types();
	foreach($current_level_up_types as $type){
		$htmls['look'][]=array('type'=>'string','name'=>'conditions.'.$type,'title'=>$level_up_types[$type]);
	}
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=$this->get('level',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['conditions']=$item['conditions']?json_decode($item['conditions'],true):array('order_price'=>'0');
		$item['commissions']=$item['commissions']?json_decode($item['commissions'],true):array('commission1'=>'0.00');
	}else{
		$item=array(
			'title'=>'',
			'conditions'=>array('order_price'=>'0'),
			'commissions'=>array('commission1'=>'0.00'),
		);
	}
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'分类设置',
		'edits'=>array(array('title'=>'基本信息','list'=>array(
			array('name'=>'title','title'=>'等级名称','type'=>'string'),
			//array('name'=>'area_type','title'=>'适用代理类型','type'=>'radio','options'=>'area_types'),
			array('name'=>'level','title'=>'等级权重','type'=>'string','help'=>'填写一百以内的数字，数字越大表示等级越高！不同等级的等级权重不得相等！'),
		))),
	);
	//升级条件表单
	$condition_edit=array(
		'title'=>'升级条件',
		'list'=>array(),
	);
	$level_up_types=$this->level_up_types;
	$current_level_up_types=$this->model()->current_level_up_types();
	foreach($current_level_up_types as $type){
		$condition_edit['list'][]=array('type'=>'string','name'=>'conditions.'.$type,'title'=>$level_up_types[$type]);
	}
	$htmls['edits'][]=$condition_edit;
	$commission_edit=array(
		'title'=>'分红比例',
		'list'=>array(),
	);
	if($this->setting['open_duodai']){
		$commission_edit['list'][]=array('type'=>'string','name'=>'commissions.duodai','title'=>'多省代比例（%）');
	}
	for($i=1;$i<=$setting['level'];$i++){
		$commission_edit['list'][]=array('type'=>'string','name'=>'commissions.commission'.$i,'title'=>$this->commission_money_types[$i].'比例（%）');
		if($this->setting['credit_type']){
			$commission_edit['list'][]=array('type'=>'string','name'=>'commissions.xuni_rate'.$i,'title'=>$i.'级获得'.$this->xuniname,'help'=>'填写数字表示实际额度，填写百分数表示比例。例如填写1表示发放1个'.$this->xuniname.'，填写1%表示发放商品价格1%的'.$this->xuniname);
		}
	}
	$htmls['edits'][]=$commission_edit;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'title'=>trim($postdata['title']),
		//'area_type'=>intval($postdata['area_type']),
		'level'=>intval($postdata['level']),
		'conditions'=>$postdata['conditions']?ijson_encode($postdata['conditions']):'',
		'commissions'=>$postdata['commissions']?ijson_encode($postdata['commissions']):'',
	);
	if($data['level']<=0 || $data['level']>100){
		exi('请填写正确的等级权重值，1-100','error');
	}
	
	$id=intval($postdata['id']);
	if($id){
		$this->update('level',$data,array('id'=>$id));
		exi('设置成功！','','list');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$this->insert('level',$data);
		exi('添加成功！','','list');
	}
}
if($_RQ['op']=='set_default'){
	//设置默认分红比例
	if($_RQ['postdata']){
		$post=get_postdata();
		$this->plugin_setting('default_level',$post);
		exi('默认等级分红比例设置成功！','','list');
	}
	$item=$this->plugin_setting('default_level');
	if(!$item){
		$item=array('commission1'=>'0.00');
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'设置默认分红比例',
		'edit'=>array(),
	);
	if($this->setting['open_duodai']){
		$htmls['edit'][]=array('type'=>'string','name'=>'duodai','title'=>'多省代比例（%）');
	}
	for($i=1;$i<=$setting['level'];$i++){
		$htmls['edit'][]=array('type'=>'string','name'=>'commission'.$i,'title'=>$this->commission_money_types[$i].'比例（%）');
		if($this->setting['credit_type']){
			$htmls['edit'][]=array('type'=>'string','name'=>'xuni_rate'.$i,'title'=>$i.'级获得'.$this->xuniname,'help'=>'填写数字表示实际额度，填写百分数表示比例。例如填写1表示发放1个'.$this->xuniname.'，填写1%表示发放商品价格1%的'.$this->xuniname);
		}
	}
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		$this->delete('level',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$this->delete('level',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>